﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace GanagroLiteBO
{
    public class SmartDate : IEditableObject, INotifyPropertyChanged, IDataErrorInfo
    {
        private DateTime _dt = System.DateTime.MinValue;
        private bool m_Editing;

        public SmartDate()
        {
            _dt = System.DateTime.MinValue;
        }


        public SmartDate(DateTime dt)
        {
            _dt = dt;
        }

        public SmartDate(string dtString)
        {
            _dt = Convert.ToDateTime(dtString);
        }

        public DateTime Fecha 
        { get { return _dt; } 
          set { _dt = value; } }

        public string Text 
            { 
            get {   return _dt.ToShortDateString(); } 
            set {
                if (_dt != Convert.ToDateTime(value))
                {
                    _dt = Convert.ToDateTime(value);
                    FirePropertyChangedNotification("Text");
                }
                }
        }

        public bool AcceptFutureDates { get; set; }

        public bool AcceptAncientDates { get; set; }

        private List<string> _warnings = new List<string>();

        public List<string> Warnings
        {
            get { return _warnings; }
        }

        private List<string> _errors = new List<string>();

        public List<string> Errors
        {
            get { return _errors; }
        }


        public bool IsValid()
        {
            bool bValid = true;

            if (!AcceptFutureDates && _dt > DateTime.Now)
            {
                bValid = false;
                Errors.Add("La fecha no puede ser en el futuro.");
            }

            if (!AcceptAncientDates && (_dt < DateTime.Now.AddYears(-1)))
            {
                bValid = false;
                Errors.Add("La fecha no puede ser anterior a un año.");
            }

            if (_dt < DateTime.Now.AddYears(-1))
            {
                Warnings.Add("Fecha es anterior a un año. Favor revisar.");
            }

            return bValid;
        }

        #region INotifyPropertyChanged Implementation
        private void FirePropertyChangedNotification(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo Members

        private string m_Error = string.Empty;
        private Hashtable m_PropErrors = new Hashtable();

        [Browsable(false)]
        public string Error
        {
            get { return m_Error; }
        }

        [Browsable(false)]
        public string this[string columnName]
        {
            get { return (string)m_PropErrors[columnName]; }
        }

        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {
            if (!m_Editing)
            {
            }

            m_Editing = true;
        }

        public void CancelEdit()
        {
            m_Editing = false;
        }

        public void EndEdit()
        {
            if (m_Editing)
            {
            }

            m_Editing = false;
        }

        #endregion
    }

    }

